// pages/store-detail/index.js
import { request } from '../../../utils/network.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    suppId: '',// 供应商的id
    supplyer:{
    //   shopVO:{
    //     supplierId: 1,
    //     shopName: '钻头精供',
    //     productsSum: 2,
    //     address:'连连大厦'
    //   },
    //   distrbutianBillVOList: [
    //     {
    //       distributionBillId : "1",
    //       orderId: "20180202102202865552",
    //       distributionStatus: "待配货",
    //       distributeTime: "2018-02-02 10:58:39",
    //       completeTime: null,
    //       supplierCompanyName: "小刀公司",
    //       supplierLinkman: "供应商01",
    //       supplierPhone: "15067144845",
    //       brandName: "大刀",
    //       goodsName: "屠龙宝刀",
    //       goodsBn: null,
    //       productBn: null,
    //       standard: null,
    //       supplyNum: 10
    //     },
    //     {
    //       distributionBillId: "2",
    //       orderId: "20180202102202865552",
    //       distributionStatus: "待配货",
    //       distributeTime: "2018-02-02 10:58:39",
    //       completeTime: null,
    //       supplierCompanyName: "小刀公司",
    //       supplierLinkman: "供应商01",
    //       supplierPhone: "15067144845",
    //       brandName: "大刀",
    //       goodsName: "屠龙宝刀",
    //       goodsBn: null,
    //       productBn: null,
    //       standard: null,
    //       supplyNum: 10
    //     },
    //     {
    //       distributionBillId: "3",
    //       orderId: "20180202102202865552",
    //       distributionStatus: "待配货",
    //       distributeTime: "2018-02-02 10:58:39",
    //       completeTime: null,
    //       supplierCompanyName: "小刀公司",
    //       supplierLinkman: "供应商01",
    //       supplierPhone: "15067144845",
    //       brandName: "大刀",
    //       goodsName: "屠龙宝刀",
    //       goodsBn: null,
    //       productBn: null,
    //       standard: null,
    //       supplyNum: 10
    //     },
    //     {
    //       distributionBillId: "4",
    //       orderId: "20180202102202865552",
    //       distributionStatus: "待配货",
    //       distributeTime: "2018-02-02 10:58:39",
    //       completeTime: null,
    //       supplierCompanyName: "小刀公司",
    //       supplierLinkman: "供应商01",
    //       supplierPhone: "15067144845",
    //       brandName: "大刀",
    //       goodsName: "屠龙宝刀",
    //       goodsBn: null,
    //       productBn: null,
    //       standard: null,
    //       supplyNum: 10
    //     },
    //     {
    //       distributionBillId: "5",
    //       orderId: "20180202102202865552",
    //       distributionStatus: "待配货",
    //       distributeTime: "2018-02-02 10:58:39",
    //       completeTime: null,
    //       supplierCompanyName: "小刀公司",
    //       supplierLinkman: "供应商01",
    //       supplierPhone: "15067144845",
    //       brandName: "大刀",
    //       goodsName: "屠龙宝刀",
    //       goodsBn: null,
    //       productBn: null,
    //       standard: null,
    //       supplyNum: 10
    //     }
    //   ]
    },
    clientHeight: 0 // 屏幕高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.setData({
      suppId: options.suppid
    })
    wx.getSystemInfo({
      success: function (res){
        that.setData({
          clientHeight: res.windowHeight
        })
      }
    })
    that.getCurrentOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCurrentOrder()
  },
  allReceiveHandle: function () {
    let that = this
    request(
      '/distributor/claimedBillBySupplierId',
      { supplierId: this.data.suppId },
      function success(data) {
        let code = data.resultCode
        if (code === 2000) {
          that.getCurrentOrder()
          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 1000
          })
        }
      },
      function fail() {
        wx.showToast({
          title: '网络连接失败',
          icon: 'none',
          duration: 1000
        })
      }
    )
  },
  getOrder: function(e) {
    let billid = e.target.dataset.billid
    request(
      '/distributor/claimedBillById',
      { distributionBillId: billid },
      function success(data) {
        let code = data.resultCode
        if (code === 2000) {
          that.getCurrentOrder()
          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 1000
          })
        }
      },
      function fail() {
        wx.showToast({
          title: '网络连接失败',
          icon: 'none',
          duration: 1000
        })
      }
    )
  },
  /**
   * 获取店铺下待配货商品
   */
  getCurrentOrder () {
    let that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 1000
    })
    request(
      '/distributor/showUnclaimedBillListBySupplierId',
      { supplierId: this.data.suppId },
      function success(data) {
        wx.hideToast()
        let code = data.resultCode
        if (code === 2000) {
          that.setData({
            supplyer: data.data
          })

        }
      },
      function fail() {
        wx.hideToast()
        wx.showToast({
          title: '网络连接失败',
          icon: 'none',
          duration: 1000
        })
      }
    )
  }
})